var service_common = require("common.js");
var user = require('user.js');
//对外公开的方法
module.exports = {
  Guanl: Guanl,
  getPageContent: getPageContent,
  saveContent: saveContent,
  delWebsite: delWebsite,
  cancelRelated: cancelRelated,
  uploadImage: uploadImage,
  createWebsite: createWebsite
}
// 关联
function Guanl(openid,id,cb){
  user.getToken(openid,token=>{
    service_common.fetchData(
      service_common.getUrl("website.relatedweb"),
      { token: token, website_id: id },
      function (res) {
        cb(res)
      },
      "GET"
    )
  })
  
}
// 获取页面
function getPageContent(openid,id,cb){
  user.getToken(openid,token=>{
    service_common.fetchData(
      service_common.getUrl("website.getPageContent"),
      { token: token, website_id: id },
      function (res) {
        cb(res)
      },
      "GET"
    )
  })
  
}
// 保存页面
function saveContent(openid, id, data,ws_nav_id,cb){
  user.getToken(openid,token=>{
    service_common.fetchData(
      service_common.getUrl("website.saveContent"),
      { token: token, website_id: id, data: JSON.stringify(data), ws_nav_id: ws_nav_id },
      function (res) {
        cb(res)
      },
      "post"
    )
  })
  
}

// 删除官网
function delWebsite(openid,id,cb){
  user.getToken(openid,token=>{
    service_common.fetchData(
      service_common.getUrl("website.delWebsite"),
      { token: token, website_id: id },
      function (res) {
        cb(res)
      },
      "GET"
    )
  })
  
}

// 取消关联
function cancelRelated(openid,id,cb){
  user.getToken(openid,token=>{
    service_common.fetchData(
      service_common.getUrl("website.cancelRelated"),
      { token: token, website_id: id },
      function (res) {
        cb(res)
      },
      "GET"
    )
  })
  
}
function uploadImage(filePath,cb){
  user.getToken(getApp().globalData.userInfo.openid,token=>{
    wx.uploadFile({
      url: service_common.getUrl("website.uploadImg"),
      filePath: filePath,
      name: 'file',
      formData: {
        "token": token
      },
      success: (res) => {
        var data = JSON.parse(res.data)
        cb(data)
      }
    })
  })
  
}

function createWebsite(short_name, name, mobile, logo,cb){
  user.getToken(getApp().globalData.userInfo.openid,token=>{
    service_common.fetchData(
      service_common.getUrl("website.createWebsite"),
      { token:token, short_name: short_name, name: name, mobile: mobile, logo: logo },
      function (res) {
        cb(res)
      },
      "GET"
    )
  })
}